function loginFun(events){
	if(events != null){		
		events.preventDefault();
	}
	var userEmail = $("#logInEmail").val();
	var userPassword = $("#logInPassword").val();

	if(userEmail.length == 0 || userEmail == null){
		alert("User Email cannot be EMPTY");
	}else if(userEmail.indexOf("@") < 1){
		alert("Invalid Email Address");
	}else if(userPassword.length == 0 || userPassword == null){
		alert("Password cannot be EMPTY");
	}else{
		$.post("http://localhost/cs2102/src/login.php", 
		{
			email: userEmail,
			password: userPassword
		},
		function(data,status){
			var userName = data;
			var userLinkHtml = generateHtml(userName);

			setCookies(userName);
			$(function(){
				$("#login").modal('hide');
				if(data.length != 2){
					$("#loginRep").html(userLinkHtml);
				}else{
					alert("Log in fail due to invalid username or password. Please try again.")
				}
			});
		});
	}
}

function generateHtml(userName){
	var userLinkHtml = "<a class = \"dropdown-toggle\" data-toggle = \"dropdown\" href = \"#\">"+userName+"<b class = \"caret\"></b></a>";
	userLinkHtml += "<ul class = \"dropdown-menu\">";
	userLinkHtml += "<li><a href = \"user.html\">"+userName+"</a></li>";
	userLinkHtml += "<li><a href=\"#\" onclick=\"logout()\">log out</a></li></ul>";
	return userLinkHtml;
}

function logout(){
	setCookies("");
	var logoutHtml = "<a href = \"#login\" data-toggle = \"modal\">Log In</a>";
	$("#loginRep").html(logoutHtml);
}

$(function(){
	var name = getCookies();
	if(name!= null && name.length > 0){
		var html = generateHtml(name);
		$("#loginRep").html(html);
	}
});